package edu.polsl.bio.str.node.delegate;

import edu.polsl.bio.str.common.exception.ManagerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * //todo class description
 * <p/>
 * Created on: Jun 14, 2011 11:34:01 AM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class NodeManagementServiceDelegateImpl extends BaseNodeDelegate implements NodeManagementServiceDelegate {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(NodeManagementServiceDelegateImpl.class.getName());

  @Override
  public long attachNode(String nodeIp, int noTokens) throws ManagerException {
    log.debug("Attaching node ");
    try {
      return getServiceLocator().getNodeManagementService().attachNode(nodeIp, noTokens);
    } catch (ManagerException me) {
      log.error("Error while attaching node");
      throw me;
    } catch (Exception e) {
      throw createCommunicationException(e);
    }
  }

  @Override
  public void detachNode(long nodeId) throws ManagerException {
    log.debug("Attaching node ");
    try {
      getServiceLocator().getNodeManagementService().detachNode(nodeId);
    } catch (ManagerException me) {
      log.error("Error while detaching node");
      throw me;
    } catch (Exception e) {
      throw createCommunicationException(e);
    }
  }

  @Override
  public void heartBeat(long nodeId) throws ManagerException {
    log.debug("Dispatching heartbeat for node = {}", nodeId);
    try {
      getServiceLocator().getNodeManagementService().heartBeat(nodeId);
    } catch (ManagerException me) {
      log.error("Error while dispatching heartbeat");
      throw me;
    } catch (Exception e) {
      throw createCommunicationException(e);
    }
  }
}
